home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / PAS_0493 / UNIXDATE.PAS < prev    next >
Pascal/Delphi Source File  |  1993-04-22  |  5KB  |  215 lines

  1. {─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 521 of 591
  3. From : Inbar Raz                           2:403/100.42         15 Apr 93  12:29
  4. To   : All
  5. Subj : UNIX TimeStamps
  6. ────────────────────────────────────────────────────────────────────────────────
  7. I saw a thread going on here, about the subject.
  8.  
  9. I just happen to have programmed such a thing, for a certain program. It's not
  10. perfect, in the essence that It will produce good results only from 1970 to
  11. 2099, because I didn't feel like starting to investigate which are leap years
  12. and which are not. All the leap years between 1970 and 2099 ARE included,
  13. though.
  14.  
  15. ---------------------------------= cut here =---------------------------------}
  16. { ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ }
  17.  
  18.   { This procedure returns a LongInt UNIX-like timestamp. TimeRec will be  }
  19.   { overwritten by the resulted UNSIGNED DWORD.                            }
  20.  
  21.   Procedure SecondSince1970(Year, Month, Day, Hour, Minute:Word; Var TimeRec);
  22.  
  23.   Var       T_Lo,
  24.             T_Hi       : Word;
  25.  
  26.   Begin
  27.     Asm
  28.  
  29.       Call @Table
  30.  
  31.   @Table:
  32.  
  33.       Pop Si
  34.       Add Si,6            { Point Si to data table }
  35.       Jmp @Compute
  36.  
  37.       { This table contains the number of days in all months UNTIL this one }
  38.  
  39.       dw  0               { Within January }
  40.       dw  31              { January }
  41.       dw  59              { February }
  42.       dw  90              { Mars }
  43.       dw  120             { April }
  44.       dw  151             { May }
  45.       dw  181             { June }
  46.       dw  212             { July }
  47.       dw  243             { August }
  48.       dw  273             { September }
  49.       dw  304             { October }
  50.       dw  334             { November }
  51.  
  52.       { This i a routine to multiply a DWORD by a WORD }
  53.       { Input: DX:AX word to multilpy, CX multiplier }
  54.  
  55.   @Calc:
  56.  
  57.       Push Si
  58.       Push Di
  59.  
  60.       Mov Di,Dx
  61.       Mov Si,Ax
  62.  
  63.       Dec Cx              { We already have it multiplied by 1 }
  64.  
  65.   @Addit:
  66.  
  67.       Add Ax,Si
  68.       Adc Dx,Di
  69.  
  70.       Loop @Addit
  71.  
  72.       Pop Di
  73.       Pop Si
  74.  
  75.       Ret
  76.  
  77.   @Compute:
  78.  
  79.       Xor Di,Di           { Variable for leap year }
  80.  
  81.       { Seconds of round years }
  82.  
  83.       Mov Bx,Year
  84.       Sub Bx,1970
  85.       Mov Ax,365*24       { Hours per year }
  86.       Mov Cx,60*60        { Seconds per hour }
  87.       Xor Dx,Dx
  88.  
  89.       Call @Calc          { Multiply dword response by CX }
  90.       Mov Cx,Bx
  91.       Call @Calc
  92.  
  93.       Push Ax
  94.       Push Dx
  95.  
  96.       { Seconds of leap years }
  97.  
  98.       Mov Ax,Year
  99.       Sub Ax,1972         { First leap year after 1972 }
  100.       Mov Bx,4
  101.       Xor Dx,Dx
  102.       Div Bx
  103.  
  104.       { DX now holds number of days to add becaues of leap years. }
  105.       { If DX is 0, this is a leap year, and we need to take it into
  106. conideration }
  107.  
  108.       Mov Di,Dx          { If DI is 0, this is a leap year }
  109.  
  110.       Inc Ax             { We must count 1972 as well }
  111.       Xor Dx,Dx
  112.       Mov Bx,60*60
  113.       Mov Cx,24
  114.  
  115.       Mul Bx
  116.       Call @Calc
  117.  
  118.       Mov Cx,Dx
  119.       Mov Bx,Ax
  120.  
  121.       { Now add what we had before }
  122.  
  123.       Pop Dx
  124.       Pop Ax
  125.  
  126.       Add Ax,Bx
  127.       Adc Dx,Cx
  128.  
  129.       Push Ax
  130.       Push Dx
  131.  
  132.       { DX:AX holds the number of seconds since 1970 till the beginning of year
  133. }
  134.  
  135.       { Add days within this year }
  136.  
  137.       Mov Bx,Month
  138.       Dec Bx
  139.       Shl Bx,1
  140.       Add Bx,Si
  141.       Mov Bx,cs:[Bx]      { Lookup Table, sum of months EXCEPT this one }
  142.       Add Bx,Day          { Add days within this one }
  143.       Dec Bx              { Today hasn't ended yet }
  144.  
  145.       Mov Ax,60*60
  146.       Mov Cx,24
  147.       Xor Dx,Dx
  148.       Mul Bx
  149.       Call @Calc
  150.  
  151.       Mov Cx,Dx
  152.       Mov Bx,Ax
  153.  
  154.       { Now add what we had before - days until beginning of the year }
  155.  
  156.       Pop Dx
  157.       Pop Ax
  158.  
  159.       Add Ax,Bx
  160.       Adc Dx,Cx
  161.  
  162.       { DX:AX now holds the number of secondss since 1970 till beginning of
  163. day. }
  164.  
  165.       Push Ax
  166.       Push Dx
  167.  
  168.       { DX:AX holds the number of seconds until the beginning of this day }
  169.  
  170.       Mov Bx,Hour
  171.       Mov Ax,60*60   { Seconds per hour }
  172.       Xor Dx,Dx
  173.       Mul Bx
  174.  
  175.       Push Ax
  176.       Push Dx
  177.  
  178.       Mov Bx,Minute
  179.       Mov Ax,60      { Seconds per minute }
  180.       Xor Dx,Dx
  181.       Mul Bx
  182.  
  183.       Mov Cx,Dx
  184.       Mov Bx,Ax
  185.  
  186.       Pop Dx
  187.       Pop Ax
  188.  
  189.       Add Bx,Ax
  190.       Adc Cx,Dx
  191.  
  192.       { And add the seconds until beginning of year }
  193.  
  194.       Pop Dx
  195.       Pop Ax
  196.  
  197.       Add Ax,Bx
  198.       Adc Dx,Cx
  199.  
  200.       { DX:AX now holds number of second since 1970 }
  201.  
  202.       Mov T_Hi,Dx
  203.       Mov T_Lo,Ax
  204.  
  205.     End;
  206.  
  207.       Move(Mem[Seg(T_Lo):Ofs(T_Lo)],
  208.            Mem[Seg(TimeRec):Ofs(TimeRec)],2);
  209.  
  210.       Move(Mem[Seg(T_Hi):Ofs(T_Hi)],
  211.            Mem[Seg(TimeRec):Ofs(TimeRec)+2],2);
  212.  
  213.   End;
  214.  
  215. { ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ }